<?php


namespace app\mall\controller;


use app\common\controller\AppCommon;
use app\mall\controller\com\Mall;
use app\service\Msg;

class Active extends Mall
{
    public function coupon()
    {
        if (!bs_p('coupon')) {
            return $this->error('未安装优惠券插件');
        }
        if (!IS_AJAX) {
            return $this->fetch();
        }
        //已过期的自动过期
        if (!$c = cache('bs_coupon_clean')) {
            //半天清理一次
            cache('bs_coupon_clean', 1, 43200);
            AppCommon::data_update('plugin_coupon', ['status' => 1, 'end_time' => ['<', time()]], ['status' => 0, 'up_time' => time()]);
        }

        $coupons = AppCommon::data_list_nopage('plugin_coupon', ['status' => 1], 'name,price,min_price,start_time,end_time,status,type,remark,code,discount', 'end_time asc,id asc');
        if ($coupons) {
            $codes = array_column($coupons, 'code');
            $log = AppCommon::data_list_nopage('plugin_coupon_log', ['uid'=>$this->uid,'code' => ['in', $codes]], 'code,status');
            if ($log) {
                $log = array_column($log, null, 'code');
            }
        }
        foreach ($coupons as $k => &$v) {
            $v['price'] = $v['price'] >= 100 ?intval($v['price']) : $v['price'];
            if ($v['end_time'] <= time()) unset($coupons[$k]);
            else {
                $v['start_time'] = date('Y.m.d', $v['start_time']);
                $v['end_time'] = date('Y.m.d', $v['end_time']);
            }
            //只可以领取一次
            $v['enabled'] = 1;
            if (isset($log[$v['code']])) $v['enabled'] = 0;
            //1-满减，2-折扣，3-无门槛
            switch ($v['type']) {
                case 1:
                    $v['type_desc'] = '满减券';
                    break;
                case 2:
                    $v['type_desc'] = '折扣券';
                    $v['price'] = round($v['discount'] / 10, 1) . '折';
                    break;
                case 3:
                    $v['type_desc'] = '无门槛券';
                    break;
                default:
                    $v['type_desc'] = '优惠券';
                    break;
            }
        }
        unset($v);
        data_return('ok', 0, $coupons);
    }

    //领取优惠券
    public function coupon_get()
    {
        if (!bs_p('coupon')) data_return('插件未启用', -1);
        if (!IS_AJAX) data_return('非法请求', -1);
        if (empty($this->param['code'])) data_return('参数有误', -1);
        $log = AppCommon::data_get('plugin_coupon_log', ['code' => $this->param['code'], 'uid' => $this->uid], 'id');
        if ($log) data_return('该券您已领取', -1);
        $coupon = AppCommon::data_get('plugin_coupon', ['code' => $this->param['code']]);
        if (!$coupon) data_return('优惠券不存在', -1);

        $res = AppCommon::data_add('plugin_coupon_log', [
            'uid' => $this->uid,
            'code' => $coupon['code'],
            'add_time' => time(),
            'status' => 0
        ]);
        if (!$res) data_return('服务正忙，请稍后', -1);
        $type = [1 => '满减券', 2 => '折扣券', 3 => '无门槛券'];

        Msg::add($this->uid, ['title' => '领取优惠券', 'content' => '恭喜您获得一张优惠券，<br>使用期限：' . date('Y-m-d H:i:s', $coupon['start_time']) . '~' . date('Y-m-d H:i:s', $coupon['end_time']) . ';<br> 优惠券码：' . $coupon['code'] . ';<br>;优惠券类型：' . $type[$coupon['type']] . '<br>使用方式：' . ($coupon['remark'] ? $coupon['remark'] : '下单抵扣') . ';<span style="color:red">每个订单至抵扣后至少需要付款0.01元；已支付订单不支持退还优惠券，未支付可以通过删除、取消订单恢复优惠券使用权限（未过期的），每张优惠券进行使用一次，售后仅支持退实付金额。</span><br> <a href="' . url('/mall/home/index') . '">去使用</a>', 'type' => 0]);
        data_return('领取成功');


    }
}